package org.gaming;

import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;
import org.come.entity.GamingQuestions;
import org.come.entity.Goodstable;
import org.come.model.Door;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

public class GamingJpanel extends JPanel implements MouseListener {

    private ImageIcon xz;
    private ImageIcon icon;
    private JLabel hihtBox;
    private Boolean b = false;
    private Goodstable goodstable;
    private GaminglBtn yz;
    private GaminglBtn tc, qx, sd, sx;
    private Door door;
    private JLabel jackpot;
    private JTextField betAmount;

    private Integer index = 0;

    private List<GaminglBtn> gaminglBtnList;

    private String bettingInformation;
    public GamingJpanel()   {
        this.setPreferredSize(new Dimension(375, 330));
        this.setLayout(null);
        this.setBackground(UIUtils.Color_BACK);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/2/8_jpg.button.btn_close.jpg", 1, 615);
        offBtn.setBounds(280, 0, 23, 23);
        this.add(offBtn);


        jackpot = new JLabel();
        jackpot.setBounds(140, 40, 60, 40);
        jackpot.setFont(UIUtils.TEXT_HY34);
        jackpot.setForeground(UIUtils.COLOR_HURTY1);
        this.add(jackpot);


        tc = new GaminglBtn("skin/8100226/button/B59.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, "头彩", 1,
                this);
        tc.setBounds(15, 100, 68, 30);
        this.add(tc);


        qx = new GaminglBtn("skin/8100226/button/B59.png", 1,  UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, "七星", 3,
                this);
        qx.setBounds(85, 100, 68, 30);
        this.add(qx);


        sx = new GaminglBtn("skin/8100226/button/B59.png", 1,  UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, "散星", 4,
                this);
        sx.setBounds(225, 100, 68, 30);
        this.add(sx);


        sd = new GaminglBtn("skin/8100226/button/B59.png", 1,  UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, "双对", 2,
                this);
        sd.setBounds(155, 100, 68, 30);
        this.add(sd);

        yz = new GaminglBtn("skin/8100226/button/B59.png", 1,  UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY16, "押注", 5,
                this);
        yz.setBounds(120, 230, 80, 30);
        this.add(yz);
        gaminglBtnList = new ArrayList<>();
        gaminglBtnList.add(qx);
        gaminglBtnList.add(sx);
        gaminglBtnList.add(sd);
        gaminglBtnList.add(tc);

        // 购买数量
        betAmount = new JTextField();
        betAmount.setFont(UIUtils.TEXT_FONT1);
        betAmount.setText("0");
        betAmount.setForeground(Color.WHITE);
        betAmount.setOpaque(false);
        betAmount.setBorder(null);
        betAmount.setBounds(120, 170, 160, 16);
        betAmount.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void removeUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void insertUpdate(DocumentEvent e) {

            }

            @Override
            public void changedUpdate(DocumentEvent e) {
            }
        });

        // 限制输入内容为数字切数字不大于100
        betAmount.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub
                int charstr = e.getKeyChar();
                String str = betAmount.getText();
                if (str.length() == 0) {
                    betAmount.setText(0 + "");
                }

                // control the value between 0 and 9
                if (charstr < KeyEvent.VK_0 || charstr > KeyEvent.VK_9) {
                    e.consume();
                }
                if (str.length() == 1) {
                    if (str.equals("0")) {
                        betAmount.setText("");
                    }
                }
//                if (str.length() > 0) {
//                    if (str.length() > 1) {
//                        if (str.length() > 2) {
//                            e.consume();
//                            betAmount.setText(999 + "");
//                        }
//                        // int sum = Integer.parseInt(betAmount.getText());
//                        // long price = Long.parseLong(Unitprice.toString());
//                        // labTotalsum.setText(price*sum+"");
//                        // Totalsum = new BigDecimal((price * sum));
//                    }
//                }
            }

            @Override
            public void keyPressed(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

        });
        this.add(betAmount);


//
//        sureBtn = new GaminglBtn("skin/8100226/button/B59.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_FONT82,"确定", 1,
//                this);
//        sureBtn.setBounds(40, 80, 34, 17);
//        this.add(sureBtn);
//
//        cancelBtn = new GaminglBtn("skin/8100226/button/B59.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_FONT82,"取消", 0,
//                this);
//        cancelBtn.setBounds(200, 80, 34, 17);
//        this.add(cancelBtn);

    }

    public void clearBorder() {
        for (GaminglBtn gaminglBtn : gaminglBtnList) {
            gaminglBtn.setBorder(BorderFactory.createEmptyBorder());
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

        if (icon == null)
            icon = new ImageIcon("inkImg/newbackground/yz.png");

        g2d.drawImage(icon.getImage(), 0, 0, null);




//        g2d.setColor(new Color(187, 165, 75));
//        g2d.setFont(UIUtils.TEXT_HYK20);
//        g2d.drawString("本局头彩", 20, 68);
//        g2d.drawString("本局头彩", 20, 60);
//        g2d.drawString("本局头彩", 20, 60);
//
//        g2d.drawString("押注金额", 20, 180);
//        g2d.drawString("押注金额", 20, 180);
//        g2d.drawString("押注金额", 20, 180);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }

    public JLabel getHihtBox() {
        return hihtBox;
    }

    public void setHihtBox(JLabel hihtBox) {
        this.hihtBox = hihtBox;
    }

    public Door getDoor() {
        return door;
    }

    public void setDoor(Door door) {
        this.door = door;
    }

    public Goodstable getGoodstable() {
        return goodstable;
    }

    public void setGoodstable(Goodstable goodstable) {
        this.goodstable = goodstable;
    }

    public JLabel getJackpot() {
        return jackpot;
    }

    public void setJackpot(JLabel jackpot) {
        this.jackpot = jackpot;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public JTextField getBetAmount() {
        return betAmount;
    }

    public void setBetAmount(JTextField betAmount) {
        this.betAmount = betAmount;
    }

    public void init(GamingQuestions gamingQuestions) {
        this.jackpot.setText(gamingQuestions.getJackpot());

        if(gamingQuestions.getStatus()){
            yz.setVisible(false);
//            bettingInformation =
        }else{
            yz.setVisible(true);
        }
    }

    public GaminglBtn getYz() {
        return yz;
    }

    public void setYz(GaminglBtn yz) {
        this.yz = yz;
    }

    public String getBettingInformation() {
        return bettingInformation;
    }

    public void setBettingInformation(String bettingInformation) {
        this.bettingInformation = bettingInformation;
    }
}
